<?php

namespace Home\Model;
use Think\Model;

class CategoryModel extends Model {
	//获取所有父分类
	public function getAllParentCategoryList($limit = false){
		//todo 缓存
		$key = md5("all_category_list_".$limit);
		if(session( md5('37degree_redis') ) === true){//清楚缓存
            S($key,null);
            $list = false;
        }else{
            $list = S($key);
        }
		if($list===false){
			$where = array('pid'=>0,'status'=>1);
			if($limit && is_numeric($limit)){
				$list = $this->field('id,name,pid,sort_id')->where($where)->order('sort_id asc')->limit($limit)->select();
			}else{
				$list = $this->field('id,name,pid,sort_id')->where($where)->order('sort_id asc')->select();	
			}
			S($key,$list,3600);
		}
		
		
		return $list;
	}

	//获取所有分类信息
	public function allCategoryList(){
		//todo 缓存
		//$key = cache_key('');
		
		$where = array('status'=>1);
		$list = $this->field('id,name,pid,sort_id')->order('pid asc,sort_id asc')->where($where)->select();

		return $list;
	}

	//获取单个分类数据
	public function getCategoryInfo($category_id = null){
		if(empty($category_id) || !is_numeric($category_id)) return null;

		$key = md5("category_info_".$category_id);

		if(session( md5('37degree_redis') ) === true){//清楚缓存
            S($key,null);
            $info = false;
        }else{
            $info = S($key);
        }

		if($info===false){
			$info = $this->field('name')->where('id='.$category_id)->find();
			
			S($key,$info,3600);	
		}
		
		return $info;
	}

}

?>